﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using System.Data;

namespace Broker.Model
{
    public class SysUser
    {
        private volatile object syncInvitedCodeQty = new object();
        private int invitedCodeQty;

        public static void CreateUser(SysUser user, ISession sess)
        {
            ITransaction tx = null;
            try
            {
                tx = sess.BeginTransaction();

                IDbCommand cmd = sess.Connection.CreateCommand();

                tx.Enlist(cmd);

                cmd.CommandText = "delete from sys_user";
                cmd.ExecuteNonQuery();

                cmd.CommandText = "delete from score_scorerecord";
                cmd.ExecuteNonQuery();

                cmd.CommandText = "update System_TableUpdate set LastTimeStamp=0  where name ='Score_ScoreRecord'";
                cmd.ExecuteNonQuery();

                sess.SaveOrUpdate(user);

                tx.Commit();

            }
            catch (Exception er)
            {
                if (tx.IsActive)
                    tx.Rollback();

                throw er;
            }
        }

        public static SysUser LoadUser(ISession sess, string name, string password)
        {
            IList<SysUser> us = sess.CreateQuery("from SysUser").List<SysUser>();

            IList<SysUser> users = sess.CreateCriteria<SysUser>().Add(Restrictions.Eq("Name", name)).Add(Restrictions.Eq("Password", password)).List<SysUser>();

            if (users.Count > 0)
                return users[0];
            else
                return null;
        }

        public SysUser()
        {
            Password = string.Empty;
        }

        /// <summary>
        /// id
        /// </summary>
        public virtual int Id { get; set; }
        /// <summary>
        /// 用户名 
        /// </summary>
        public virtual string Name { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        public virtual string Nickname { get; set; }
        /// <summary>
        /// 机构名称
        /// </summary>
        public virtual string OrganizationName { get; set; }
        /// <summary>
        /// 登陆密码
        /// </summary>
        public virtual string Password { get; set; }
        /// <summary>
        /// 邀请码数量
        /// </summary>
        public virtual int InvitedCodeQty 
        {
            get
            {
                lock (syncInvitedCodeQty)
                {
                    return invitedCodeQty;
                }
            }
            set
            {
                lock (syncInvitedCodeQty)
                {
                    this.invitedCodeQty = value;
                }
            }
        }

        /// <summary>
        /// 积分
        /// </summary>
        public virtual double Score { get; set; }
        /// <summary>
        /// 检查密码
        /// </summary>
        /// <param name="sf"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public virtual bool CheckLoginPassword(ISession sess, string password)
        {
            return password == this.Password ? true : false;
        }

    }
}
